package dao.implementation;


import java.util.HashMap;

import dao.AuthorDAO;
import dao.UserDAO;
import data.Author;
import data.User;
import utils.DAOFactory;
import utils.JDBCUtil;


public class AuthorDAOImplementation implements AuthorDAO
{
	/**
	 * user_id从1开始
	 * */
	private int selectIDByPenName(String pen_name)
	{
		String condition = AuthorDAO.COLUMN_NAME_PEN_NAME
				+ "='"
				+ pen_name
				+ "'";
		return (int)JDBCUtil.selectValue
				(
				AuthorDAO.TABEL_NAME,
				AuthorDAO.COLUMN_NAME_USER_ID,
				condition
				);
	}
	
	/**
	 * @return 返回的值不会是""
	 * */
	private String selectPenNameByID(int user_id)
	{
		String condition = AuthorDAO.COLUMN_NAME_USER_ID
				+ "='"
				+ user_id
				+ "'";
		return (String)JDBCUtil.selectValue
				(
				AuthorDAO.TABEL_NAME,
				AuthorDAO.COLUMN_NAME_USER_ID,
				condition
				);
	}
	
	
	@Override
	public Author selectByID(int user_id)
	{
		String pen_name = this.selectPenNameByID(user_id);
		if (pen_name == null)
			return null;
		
		UserDAO dao_user = (UserDAO)DAOFactory.getDAOInstance(DAOFactory.DAOClassName.USER);
		User user = dao_user.selectByID(user_id);
		if (user == null)
			return null;
		
		Author author = new Author(user, pen_name);
		
		return author;
	}


	@Override
	public Author selectByPenName(String pen_name)
	{
		int user_id = this.selectIDByPenName(pen_name);
		if (user_id == 0)//user_id从1开始
			return null;
		
		UserDAO dao_user = (UserDAO)DAOFactory.getDAOInstance(DAOFactory.DAOClassName.USER);
		User user = dao_user.selectByID(user_id);
		if (user == null)
			return null;
		
		Author author = new Author(user, pen_name);
		
		return author;
	}


	@Override
	public Author selectByUser(User user)
	{
		return this.selectByID(user.getID());
	}
	
	
	@Override
	public Author selectByUsername(String username)
	{
		UserDAO dao_user = (UserDAO)DAOFactory.getDAOInstance(DAOFactory.DAOClassName.USER);
		User user = dao_user.selectByUsername(username);
		if (user == null)
			return null;
		
		String pen_name = this.selectPenNameByID(user.getID());
		return new Author(user, pen_name);
	}
	
	
	@Override
	public void insert(Author author)
	{
		JDBCUtil.insert
			(
				AuthorDAO.TABEL_NAME,
				String.valueOf(author.getID()),
				author.getPenName(),
				String.valueOf(author.getFans()),
				String.valueOf(author.getWorksCount()),
				String.valueOf(author.getBooksCount())
			);
	}
	
	
	@Override
	public void insert(int user_id, String pen_name)
	{
		HashMap<String, String> key_value = new HashMap<String, String>();
		key_value.put(AuthorDAO.COLUMN_NAME_USER_ID, String.valueOf(user_id));
		key_value.put(AuthorDAO.COLUMN_NAME_PEN_NAME, pen_name);
		
		JDBCUtil.insert(AuthorDAO.TABEL_NAME, key_value);
	}
	
	
	@Override
	public void update(Author author)
	{
		HashMap<String, String> map = new HashMap<String, String>();
		map.put(AuthorDAO.COLUMN_NAME_USER_ID, String.valueOf(author.getID()));
		map.put(AuthorDAO.COLUMN_NAME_PEN_NAME, author.getPenName());
		map.put(AuthorDAO.COLUMN_NAME_COUNT_FANS, String.valueOf(author.getFans()));
		map.put(AuthorDAO.COLUMN_NAME_COUNT_WORKS, String.valueOf(author.getWorksCount()));
		map.put(AuthorDAO.COLUMN_NAME_COUNT_BOOKS, String.valueOf(author.getBooksCount()));
		
		
		String condition = AuthorDAO.COLUMN_NAME_USER_ID
				+ "='"
				+ author.getID()
				+ "'";
		JDBCUtil.update(AuthorDAO.TABEL_NAME, map, condition);
	}


	@Override
	public void update(int user_id, String column_name, String value)
	{
		String condition = AuthorDAO.COLUMN_NAME_USER_ID
				+ "='"
				+ user_id
				+ "'";
		JDBCUtil.updateOneValue(AuthorDAO.TABEL_NAME, column_name, value, condition);
	}
	
	
	@Override
	public boolean isAuthor(int user_id)
	{
		System.out.println(this.toString() + ".isAuthor");
		
		String pen_name = this.selectPenNameByID(user_id);
		if (pen_name == null)
			return false;
		return true;
		
	}
	
	
	@Override
	public boolean isAuthor(String username)
	{
		System.out.println(this.toString() + ".isAuthor");
		if (this.selectByUsername(username) == null)
			return false;
		return true;
	}
	
	
	@Override
	public boolean hasAuthor(String pen_name)
	{
		System.out.println(this.toString() + ".hasAuthor");
		
		int user_id = this.selectIDByPenName(pen_name);
		if (user_id == 0)
			return false;
		return true;
	}


	


	
	
	
}







